package com.pateo.bjev.contacts.utils;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.CalendarContract;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.pateo.bluetooth.logic.contact.manager.ContactsManager;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by pateo on 17-5-12.
 */

public class CallUtils {

    public static void call(Context context, String number) {
        if (number == null || !TextUtils.isGraphic(number)) {
            // There is no number entered.
            return;
        }
        ContactsManager manager = ContactsManager.getInstance(context.getApplicationContext());
        if (!manager.isBluetoothConnect()) {
            return;
        }
        Intent intent = new Intent(Intent.ACTION_CALL, Uri.fromParts("tel",
                number, null));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        context.startActivity(intent);
    }

    public static String formatTime(String time){
        SimpleDateFormat sdf  = new SimpleDateFormat("yyyyMMddHHmmsss");
        SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat hourFormat = new SimpleDateFormat("HH:mm");
        try {
            Calendar calendar = Calendar.getInstance();
            Date date = sdf.parse(time);
            calendar.setTime(date);

            long time_call = calendar.getTimeInMillis();
            Date nowDate = new Date();
            calendar.setTime(nowDate);
            long time_now = calendar.getTimeInMillis();

            //得到两个日期相差的天数
            int days = (int) ((time_now - time_call)/1000/3600/24);
            if(days>=7){
                return outFormat.format(date);
            }else if(days >=2 && days <=6){
                return days+"天前";
            }else if(days == 1){
                return "昨天";
            }else{
                return hourFormat.format(date);
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static void hidenOrShowInputMethod(Context context,boolean show) {
        InputMethodManager inputMethodManager = (InputMethodManager) context.getApplicationContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
//        if(show){
//            inputMethodManager.showSoftInput(view,InputMethodManager.SHOW_FORCED);
//        }else{
//            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),0);
//        }
        if(inputMethodManager.isActive()){
            if(!show){
                inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
            }
        }else{
            if(show){
                inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
            }
        }
//        inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    }
}
